package com.baykalcakici.cafepicker.util;

import java.net.URLEncoder;
import java.util.HashMap;

public class FtClient {
  public static final String REQUEST_URL = "https://www.google.com/fusiontables/api/query";
  private String token;

  public FtClient(String token) {
    this.token = token;
  }

  public String query(String query) {
    String result = "";

    // Create the auth header
    HashMap<String, String> headers = new HashMap<String, String>();
    headers.put("Authorization", "GoogleLogin auth=" + this.token);

    // Convert to lower for comparison below
    String lower = query.toLowerCase();
    // Encode the query
    query = "sql=" + URLEncoder.encode(query);

    // Determine POST or GET based on query
    if (lower.startsWith("select") || lower.startsWith("show") || lower.startsWith("describe")) {
      result =
          RequestHandler.sendHttpRequest(FtClient.REQUEST_URL + "?" + query, "GET", null, headers);
    } else {
      result = RequestHandler.sendHttpRequest(FtClient.REQUEST_URL, "POST", query, headers);
    }

    return result;
  }

}
